package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.SetmealDish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "套餐相关接口")
@RestController
@RequestMapping("/admin/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", key = "#setmealDTO.categoryId") // 根据分类id清空当前分类下所有套餐缓存
    @ApiOperation("新增套餐")
    @PostMapping
    public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @ApiOperation("套餐查询")
    @GetMapping("/page")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult pageResult = setmealService.page(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 修改套餐
     * @param setmealDTO
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
    @ApiOperation("修改套餐")
    @PutMapping
    public Result<String> update(@RequestBody SetmealDTO setmealDTO) {
        setmealService.update(setmealDTO);
        return Result.success();
    }

    /**
     * 根据id获取套餐
     * @param setmealId
     * @return
     */
    @ApiOperation("获取菜品套餐")
    @GetMapping("/{setmealId}")
     public Result<SetmealVO> getSetmeal(@PathVariable Long setmealId) {
        SetmealVO setmealVO = setmealService.getSetmeal(setmealId);
        return Result.success(setmealVO);
     }

    /**
     * 启用、禁用套餐
     * @param status
     * @param id
     * @return
     */
    @CacheEvict(cacheNames = "setmeal", allEntries = true)
     @ApiOperation("启用、禁用套餐")
     @PostMapping("/status/{status}")
     public Result<String> startOrStop(@PathVariable Integer status, Long id) {
        setmealService.startOrStop(status, id);
        return Result.success();
     }

    /**
     * 删除套餐
     * @param ids
     * @return
     */
     @CacheEvict(cacheNames = "setmeal", allEntries = true)
     @ApiOperation("删除套餐")
     @DeleteMapping
     public Result<String> delete(@RequestParam List<Long> ids) {
         setmealService.delete(ids);
         return Result.success();
     }

}
